<template>
  <el-dialog title="物料产品选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="80%"
    center
  >

    <el-row :gutter="20">
      <!--物料数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="物料名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入物料名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table border ref="itemTable" v-loading="loading" :data="itemList"  @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column label="订单编号" width = "120" align="center" key="orderCode" prop="orderCode" v-if="columns[0].visible" />
          <el-table-column label="到货单号" width = "120" align="center" key="arrivedCode" prop="arrivedCode" v-if="columns[1].visible" />
          <el-table-column label="物料编码" width = "120" align="center" key="itemCode" prop="itemCode" v-if="columns[2].visible" />
          <el-table-column label="物料名称" min-width="120" align="left" key="itemName" prop="itemName" v-if="columns[3].visible" :show-overflow-tooltip="true" />
          <el-table-column label="规格型号" align="left" key="specification" prop="specification" v-if="columns[4].visible" :show-overflow-tooltip="true" />
          <el-table-column label="单位" align="center" key="unitOfMeasure" prop="unitOfMeasure" v-if="columns[5].visible" :show-overflow-tooltip="true" />
          <el-table-column label="所属分类" align="center" key="itemTypeName" prop="itemTypeName" v-if="columns[6].visible" width="120" />
          <el-table-column
            v-for="col in columnsConfig"
            :key="col.prop"
            :label="col.label"
            :prop="col.prop"
            :width="col.width"
            :align="col.align"
            v-if="col.show">
          </el-table-column>
          <el-table-column label="客户" align="center" key="clientName" prop="clientName"  width="120" />
          <el-table-column label="最大入库数量" align="center" prop="maxQuantityRecived" v-if="columns[11].visible" width="160"/>
        </el-table>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="cancelSelect()">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getArrivedLineList} from "@/api/mes/wm/itemrecpt";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";


export default {
  name: "CgOrderDetailSingle",
  props: {
    vendorId:null,
    arrivedCode:null,
    quantityRecived:{}
  },
  data() {
    return {
      loading: false,
      showFlag:false,
      selectedRows: undefined,
      // 列表中不存在的数据
      selectedRows1:[],
      //父页面传回的数据
      selectedRows2:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      queryParams: {
        orderCode: undefined,
        arrivedCode: this.arrivedCode,
        itemName: undefined,
      },
      // 列信息
      columns: [
        { key: 0, label: `订单编码`, visible: true },
        { key: 1, label: `到货单号`, visible: true },
        { key: 2, label: `物料/产品编码`, visible: true },
        { key: 3, label: `物料/产品名称`, visible: true },
        { key: 4, label: `规格型号`, visible: true },
        { key: 5, label: `单位`, visible: true },
        { key: 6, label: `物料分类`, visible: true },
        { key: 7, label: `品牌`, visible: true },
        { key: 8, label: `颜色`, visible: true },
        { key: 9, label: `技术要求`, visible: true },
        { key: 10, label: `型号`, visible: true },
        { key: 11, label: `最大入库数量`, visible: true },
      ],
      columnsConfig:[],
    };
  },
  created() {
    /*this.getList();*/
    this.getItemBase();
  },
  watch: {
    arrivedCode(newVal) {
      if (newVal) {
        this.queryParams.arrivedCode = newVal;
        this.getList();
      }
    }
  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      getArrivedLineList(this.queryParams).then(response => {
        if(response.data){
          response.data.forEach(item=>{
            if(this.quantityRecived[item.arrivedLineId]){
              item.maxQuantityRecived = item.maxQuantityRecived + this.quantityRecived[item.arrivedLineId];
            }
          })
        }
          this.itemList = response.data;
          this.loading = false;
          this.$nextTick(function (){
            if(this.selectedRows2.length>0){
              this.selectedRows2.forEach(row=>{
                let data = this.itemList.find(item=>item.arrivedLineId==row.arrivedLineId);
                if(data){
                  this.$refs.itemTable.toggleRowSelection(data);
                }else{
                  row.maxQuantityRecived=row.quantityRecived;
                  this.selectedRows1.push(row);
                }

              })
            }
          })
        }
      );
    },
    handleSelectionChange(val){
        val.forEach(item=>{
          item.warehouseInfo=[];
          item.quantityRecived=item.maxQuantityRecived;
        })
      this.selectedRows = val;
    },
    //确定选中
    confirmSelect: function () {
      if (this.selectedRows == null || this.selectedRows.length == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.selectedRows=this.selectedRows.concat(this.selectedRows1);
      this.$emit('onSelected', this.selectedRows);
      this.cancelSelect();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.itemName=null;
      this.queryParams.orderCode=null;
      this.queryParams.arrivedCode=null;
      this.handleQuery();
    },
    cancelSelect(){
      this.selectedRows1=[];
      this.showFlag = false;
    },
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
  },
};
</script>
